#include "p_file_utils.h"

#include "p_error.h"

#include <stdio.h>

std::string file_read(const std::string &file_name)
{
   if (file_exists(file_name)) {
      // TODO: Rework the implementation for faster adding to the string
      // TODO: Preallocating memory for faster reading from a file.
      std::string result = "";

      FILE *file = fopen(file_name.c_str(), "r");
      char c;
      while ((c = fgetc(file)) != EOF)
         result += c;

      fclose(file);

      return result;
   } else {
      throw error_struct_create("Unable to open file '" + file_name + "'!");
   }
}

bool file_exists(const std::string &file_name)
{
   FILE *file = fopen(file_name.c_str(), "r");
   if (file) {
      fclose(file);
      return true;
   }
   return false;
}
